chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    var data = JSON.parse(request);
    console.log('------------' + data.url + ' 请求性能分析------------');
    console.log('php脚本执行总时长：' + data.total[0].time + 'ms');

    //total
    if (data.customized) {
      console.log('php脚本执行自定义分析：');
      var total_customized_time = 0;
      for (var index = 0; index < data.customized.length; index++) {
        if (data.customized[index].time) {
          total_customized_time += data.customized[index].time;
          console.log(data.customized[index].data + '耗时：' + data.customized[index].time + 'ms');
        }
      }
      console.log('');
    }

    //db
    if (data.db) {
      console.log('php脚本执行查询数据库：');
      var query_time = 0;
      var total_db_time = 0;
      for (var index = 0; index < data.db.length; index++) {
        query_time ++;
        total_db_time += data.db[index].time;
        console.log(data.db[index].data + '耗时：' + data.db[index].time + 'ms');
      }
      console.log('总共查询数据库 ' + query_time + ' 次');
      console.log('查询数据库耗时 ' + total_db_time.toFixed(2) + ' ms');
      console.log('');
    }

    //cache
    if (data.cache) {
      console.log('php脚本查询缓存：');
      var cache_time = 0;
      var total_cache_time = 0;
      for (var index = 0; index < data.cache.length; index++) {
        if (data.cache[index].data != 'init redis') {
          cache_time ++;
        }
        total_cache_time += data.cache[index].time;
        console.log(data.cache[index].data + ' 耗时：' + data.cache[index].time + 'ms');
      }
      console.log('总共查询缓存 ' + cache_time + ' 次');
      console.log('查询缓存耗时 ' + total_cache_time.toFixed(2) + ' ms');
      console.log('');
    }

    //http
    if (data.http) {
      console.log('php脚本发起http请求：');
      var http_time = 0;
      var total_http_time = 0;
      for (var index = 0; index < data.http.length; index++) {
        http_time ++;
        total_http_time += data.http[index].time;
        console.log(data.http[index].data + ' 耗时：' + data.http[index].time + 'ms');
      }
      console.log('总共请求 ' + http_time + ' 次');
      console.log('http请求耗时 ' + total_http_time.toFixed(2) + ' ms');
      console.log('');
    }

    console.log('------------分析结束------------');
    console.log('');
    console.log('');
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
  });